import oss2
import json
import os

auth = oss2.Auth('LTAIP816Jan0Nvjq', 'lm9mo4aBFhzjvP8CejjEP3d7HPEi9n')
bucket = oss2.Bucket(auth, 'https://oss-cn-zhangjiakou.aliyuncs.com', 'ram-agriinsurance-prod')


def read_json(path):
    file = open(path, "rb")
    m_json = json.load(file)
    return m_json


def down_pic(path, name):
    # 保存文件
    bucket.get_object_to_file(path, name)


def make_dir(path):
    isExists = os.path.exists(path)
    if not isExists:
        os.makedirs(path)

if __name__ == "__main__":
    file_json = read_json("target.json")
    for record in file_json:
        dkbm = record["dkbm"]
        nhxm = record["nhxm"]
        make_dir(nhxm)
        nhqzs=json.loads(record["nhqz"])
        xczps=json.loads(record["xczp"])
        bdzlts=json.loads(record["bdzlt"])
        index=1
        for nhqz in nhqzs:
            down_pic(nhqz["path"], nhxm + "/" + "农户签字("+str(index)+")" + dkbm + ".jpg")
            index=index+1

        index = 1
        for xczp in xczps:
            down_pic(xczp["path"], nhxm + "/" + "现场照片("+str(index)+")" + dkbm + ".jpg")
            index = index + 1

        index = 1
        for bdzlt in bdzlts:
            down_pic(bdzlt["path"], nhxm + "/" + "坐落图("+str(index)+")" + dkbm + ".jpg")
            index = index + 1
        print(dkbm)
